package com.yunxi.service.dubboService.impl.sat;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.model.sat.SatTarget;
import com.yunxi.sat.pojo.SatTargetVO;
import com.yunxi.sat.service.SatTargetDubboService;
import com.yunxi.service.localService.sat.SatTargetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2019-07-24
 */
public class SatTargetDubboServiceImpl implements SatTargetDubboService {

    @Autowired
    @Qualifier(value = "satTargetServiceImpl")
    SatTargetService satTargetService;


    @Override
    public SatTargetVO getById(String id) throws InvocationTargetException, IllegalAccessException {
        SatTarget satTarget = satTargetService.getById(id);
        SatTargetVO satTargetVO = new SatTargetVO();
        BeanUtils.copyProperties(satTarget, satTargetVO);
        return satTargetVO;
    }

    @Override
    public void saveOrUpdate(SatTargetVO satTargetVO,LoginInfo loginInfo) throws InvocationTargetException, IllegalAccessException {
        Object partner = loginInfo.getPartner();
        if(partner != null){
            PartnerVO partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partner), PartnerVO.class);
            satTargetVO.setPartnerCode(partnerVO.getPartnerCode());
        }
        SatTarget satTarget = new SatTarget();
        BeanUtils.copyProperties(satTargetVO, satTarget);
        satTargetService.saveOrUpdate(satTarget);
    }

    @Override
    public void removeById(String id) {
        satTargetService.removeById(id);
    }

    @Override
    public PageInfo<SatTargetVO> findPageByVO(SatTargetVO satTargetVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
        PageInfo<SatTargetVO> pageInfo = satTargetService.pageByVo(satTargetVO, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 厂端目标分页
     * @param satTargetVO
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SatTargetVO> factoryPage(SatTargetVO satTargetVO, Integer pageNum, Integer pageSize) throws Exception {
        if(StringUtils.isEmpty(satTargetVO.getYm()))throw new Exception("操作失败,请选择任务月度!");
        satTargetVO.setTenantId("pms");
        PageInfo<SatTargetVO> pageInfo = satTargetService.factoryPage(satTargetVO, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public void removeByListVo(List<SatTargetVO> satTargetVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        satTargetService.removeByListVo(satTargetVOList);
    }

    @Override
    public List listByVo(SatTargetVO satTargetVO) {
        return satTargetService.listByVo(satTargetVO);
    }

    @Override
    public List listByPartner(SatTargetVO satTargetVO, LoginInfo loginInfo) {
        Map map = new HashMap();
        map.put("partnerCode", loginInfo.getPartnerCode());
        map.put("ym", satTargetVO.getYm());
        map.put("tenantId", "pms");
        return satTargetService.listByPartner(map);
    }

    @Override
    public void saveByPartner(List<SatTargetVO> satTargetVOList, LoginInfo loginInfo) {
        satTargetService.saveByPartner(satTargetVOList, loginInfo);
    }

    /**
     * 厂端目标维护
     * @param list
     * @throws Exception
     */
    @Override
    public void newTarget(List<SatTargetVO> list) throws Exception {
        if(list == null || list.size() <= 0)throw new Exception("操作失败,list为空!");
        for(SatTargetVO vo : list){
            QueryWrapper<SatTarget> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PARTNER_CODE",vo.getPartnerCode());
            queryWrapper.eq("YM",vo.getYm());
            queryWrapper.eq("TARGET_TYPE",0);
            if(StringUtils.isNotEmpty(vo.getTargetId())){
                queryWrapper.ne("TARGET_ID",vo.getTargetId());
            }
            List<SatTarget> list1 = satTargetService.list(queryWrapper);
            if(list1 != null && list1.size() > 0){
                throw new Exception("操作失败,渠道:"+vo.getPartnerCode()+vo.getYm()+"目标已存在!");
            }
            SatTarget satTarget = new SatTarget();
            BeanUtils.copyProperties(vo,satTarget);
            satTarget.setTargetType(0);
            satTargetService.saveOrUpdate(satTarget);
        }
    }

}
